﻿if (typeof Modules == "undefined") Modules = {};
if (typeof Modules.Core == "undefined") Modules.Core = {};

Modules.Core.Default = {
    Image: function () {
        return "../../Images/no_image.jpg";
    },
    TemplatePath: '../../uploads/template/',
    TemplateViewDefault: null,
    TemplateView: function () {
        if (this.TemplateViewDefault == null) {
            this.TemplateViewDefault = $.GetURL('VTNo') || '01';
        }
        return this.TemplateViewDefault;
    }
};
Modules.Core.Cookie = {
    LanguageID: function (pValue) {
        var cookieName = "VTNT:LanguageID";
        if (pValue) {
            $.SetCookie(cookieName, pValue);
        }
        else {
            return $.GetCookie(cookieName) || "129";
        }
    }
};
Modules.Core.QueryString = {
    ResourceID: function () {
        return $.GetURL("ResourceID");
    },
    ResourcePID: function () {
        return $.GetURL("ResourcePID");
    },
    ResourceType: function () {
        return $.GetURL("ResourceType");
    },
    PageType: function () {
        return $.GetURL("PageType");
    }
};
Modules.Core.Service = {
    Get: function (pUrl, pOptions, pCallback) {
        if (typeof pOptions == "undefined") {
            return false;
        }
        var options = {
            RootName: "InputValue",
            LanguageID: Modules.Core.Cookie.LanguageID()
        };
        if ($.isArray(pOptions)) {
            pOptions.push({
                RootName: "InputValue",
                LanguageID: Modules.Core.Cookie.LanguageID()
            });
            options = pOptions;
        }
        else {
            $.extend(true, options, pOptions);
        }
        var inputValue = $.GetXmls(options);
        console.log(inputValue);
        $.Ajax({
            url: pUrl,
            type: "POST",
            isServiceData: 1,
            data: {
                inputValue: $.HtmlEncode(inputValue)
            },
            success: function (data, textStatus, XMLHttpRequest) {
                if (data) {
                    data = $.FindInXml(data);
                    if (data) {
                        data = data.CSV2JSON2();
                    }
                }
                if (typeof pCallback == "function") {
                    pCallback(data);
                }
            }
        });
    },
    GetResource: function (pOptions, pCallback) {
        this.Get('Core/CoreService.asmx/GetResource', pOptions, pCallback);
    },
    GetContextData: function (pOptions, pCallback) {
        this.Get('Core/CoreService.asmx/GetContextData', pOptions, pCallback);
    }
};

//------------------------------------------------------------------------------
//Create by: dolamthien@gmail.com
//Create date: 2014/03/05
//------------------------------------------------------------------------------
Modules.ServiceData = {
    Get: function (pUrl, pInputValue, pCallback) {
        $.Ajax({
            url: pUrl,
            type: "POST",
            isServiceData: 1,
            data: {
                inputValue: $.HtmlEncode(pInputValue)
            },
            success: function (data, textStatus, XMLHttpRequest) {
                if (data) {
                    data = $.FindInXml(data);
                    if (data) {
                        data = data.CSV2JSON2();
                    }
                }
                if (typeof pCallback == "function") {
                    pCallback(data);
                }
            }
        });
    },
    GetContextData: function (pInputValue, pCallback) {
        //dlt
        //inputCore: Chỗ này sẽ quản lý ngôn ngữ đang dùng để truyền vào & các tham số core khác
        var inputCore = '<InputValue LanguageID="129" />';

        pInputValue = inputCore + pInputValue;
        this.Get('Core/CoreService.asmx/GetContextData', pInputValue, pCallback);
    }
};

Modules.Resource = {
    Get: function (pOptions, pCallback) {
        if (pOptions.rID != null && pOptions.rID != '') {
            var rID = pOptions.rID,
                rType = pOptions.rType,
                template = '<RequestParams ID="{0}" Type="{1}" Context="GetResource" />';
            var input = $.string.Format(template, rID, rType);
            Modules.ServiceData.GetContextData(input, pCallback);
        }
    }
};